package cn.whut.glab.dao;

import java.sql.SQLException;
import java.util.List;

import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import cn.whut.glab.domain.BD_Permission;
import cn.whut.glab.domain.BD_Teacher;
import cn.whut.glab.utils.DataSourceUtils;
import cn.whut.glab.utils.StringUtil;


public class TeacherDao {

	public List<BD_Teacher> getTeacherList(int rows, int page,BD_Teacher teacher) throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql1 = "select * from BD_Teacher ";
		if(teacher!=null&&StringUtil.isNotEmpty(teacher.getName())){
			sql1=sql1+" where Name like '%"+teacher.getName()+"%'";
		}
		String sql2=" limit "+( page-1) + "," + rows;
		String sql=sql1+sql2;
		return runner.query(sql, new BeanListHandler<BD_Teacher>(BD_Teacher.class));
	}
	
	public int getTeacherCount() throws SQLException {
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql = "select count(1) from BD_Teacher";
		Long query = (Long) runner.query(sql, new ScalarHandler());
		return query.intValue();
	}

	public void deleteTeacher(String Id) throws SQLException{
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql="delete from BD_Teacher where Id='"+Id+"'";
		//System.out.println(sql);
		runner.update(sql);
	}
	
	public void addTeacher(BD_Teacher teacher) throws SQLException{
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql="insert into BD_Teacher(Name,Gender,Birthday,NativePlace,Tel,Email,QQ) values('" + teacher.getName()
					+ "','" + teacher.getGender() + "','" + teacher.getBirthday() + "','" + teacher.getNativePlace()
					+ "','" + teacher.getTel() + "','" + teacher.getEmail() + "','" + teacher.getQQ() + "')";
		//System.out.println(sql);
		runner.update(sql);
	} 
	
	public void editTeacher(BD_Teacher teacher, String Id) throws SQLException{
		QueryRunner runner = new QueryRunner(DataSourceUtils.getDataSource());
		String sql="update BD_Teacher set Name='" + teacher.getName() + "',Gender='" + teacher.getGender()
					+ "',Birthday='" + teacher.getBirthday() + "',NativePlace='" + teacher.getNativePlace() + "',Tel='"
					+ teacher.getTel() + "',Email='" + teacher.getEmail() + "',QQ='" + teacher.getQQ() + "' where Id='"+Id+"'";
		runner.update(sql);
	}


}
